import axios from 'axios'
import Vue from 'vue'
import {baseURL_API} from "@/env";
window._axiosPromiseArr = []
const request = axios.create({
    timeout: 6000 //普通的请求 6秒之内
})
// 请求拦截器
request.interceptors.request.use(function (config) {
    config.baseURL = baseURL_API;
    return config
}, error => {
    console.log(error, 'request to resolve')
    return Promise.resolve({})
})

// 响应拦截器
request.interceptors.response.use(function (response) {
    response.cancelToken = new axios.CancelToken(cancel =>  window._axiosPromiseArr.push({cancel}));
    const status = response.data.errorCode;
    switch (status) {
        case '200':
            break
        default:
            let msg = response.data.errorMessage || response.data.ErrorMessage
            Vue.prototype.$toast({
                message: msg,
                type: 'error'
            })
    }
    return response.data || response
}, error => {
    console.log(error, 'response to resolve')
    return Promise.resolve({})
})

export default request
